package com.booth.manage.controllers;


import com.alibaba.fastjson.JSON;
import com.booth.base.controllers.BaseController;
import com.booth.base.model.Users;
import com.booth.base.utils.ResultStatus;
import com.booth.manage.model.Booth;
import com.jfinal.plugin.activerecord.Page;
/***
 */
public class BoothController extends BaseController
{
	private boolean success = false;
	/***
	 * 进入列表页
	 */
	public void index()
	{
		render("booth_index.jsp");
	}
	/***
	 * 根据id是否为空，为空进入新增页，不为空进入修改页
	 */
	public void au_page()
	{
		Integer selectId = getParaToInt("selectId",0);
		if(selectId != null && selectId > 0)
		{
			Booth item = Booth.dao.findById(selectId);
			setAttr("item", item);
		}
		render("booth_au.jsp");
	}
	/***
	 * 获取列表页数据
	 */
	public void list()
	{
		String name=getPara("name");
		String num=getPara("num");
		String status=getPara("status");
		int page = getParaToInt("page",1);
		int pageSize = getParaToInt("rows",10);
		Page<Booth> pager = Booth.dao.findPage(page, pageSize,name,num,status);
		setAttr("total",pager.getTotalRow());
		setAttr("rows",pager.getList());
		renderResult(ResultStatus.SUCCESS);
	}
	/***
	 * 根据id是否为空判断为新增还是修改，为空则为新增，不为空则为修改
	 */
	public void save()
	{
		Users u = getCurUser();
		System.out.println("a=:"+JSON.toJSONString(u));
		Booth booth = getModel(Booth.class);
		if(booth.getInt("id") == null || booth.getInt("id") == 0 )
		{
			booth.set("status_cn", "启用");
			setCreator(booth, u);
			success = booth.save();
			if(success)
				renderResult(ResultStatus.SUCCESS);
			else
				renderResult(ResultStatus.ADD_FAILURL);
		}
		else
		{
			
			setOperator(booth, u);
			success = booth.update();
			if(success)
				renderResult(ResultStatus.SUCCESS);
			else
				renderResult(ResultStatus.UPDATE_FAILURE);
		}
	}
	/***
	 * 删除
	 */
	public void delete()
	{
		Integer selectId = getParaToInt("selectId",0);
		if(selectId > 0)
		{
			
			success = Booth.dao.deleteById(selectId,getCurUser());
			if(success)
				renderResult(ResultStatus.SUCCESS);
			else
				renderResult(ResultStatus.DELETE_FAILURE);
		}
	}
	public void doOpen()
	{
		Integer selectId = getParaToInt("selectId",0);
		if(selectId > 0)
		{
			
			Integer status = getParaToInt("status",1);
			
			success = Booth.dao.updateById(selectId,status,getCurUser());
			if(success)
				renderResult(ResultStatus.SUCCESS);
			else
				renderResult(ResultStatus.DELETE_FAILURE);
		}
	}
	
	/**
	 *  摊位下拉框
	 */
	public void showBoothCombobox()
	{
		String sql = "select t.id,CONCAT('名称：[',t.`name`,'],编号：[',t.num,'],面积：[',t.area,'],地址：[',t.address,']') as text from booth t where t.is_delete = 0\n" + 
				"and not EXISTS(\n" + 
				"	select 1 from booth_progress a where a.progress_status in (0,1) and a.booth_status = 0\n" + 
				"	and t.id = a.booth_id\n" + 
				")";
		renderJson(Booth.dao.find(sql));
	}

}
